#!/usr/bin/perl -w
use strict;
use Getopt::Long;

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    panda_create_pi_fasta.pl
                                --temp_dir /panda_tmp
                                --results /home/panda_results/panda.fasta
                                --db_login "dbname db_user"
                                [--where "WHERE sequences.len=45"]
                                [--help]

        'dbname' is the database name and 'db_user' is a valid user
        --temp_dir should be read/write accessible to postgreSQL
        --where is an optional parameter to filter the sequences
             using a SQL 'WHERE' clause.
             In this case, sequences are included only
             if they have lengths of 45.
	
RESULTS:

 --results   All matching sequences in panda sorted by panda ID.
             The accession line will contain the panda ID.

   This script extracts all sequences from the panda database
       tagged by panda numbers on the accession line.

USAGE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
my $TEMP_DIR;
my $DB_LOGIN;
my $where = '';
my $results = '';
my $help;
GetOptions(
			'results=s'			=> \$results,
			'temp_dir=s'		=> \$TEMP_DIR,
			'db_login=s'		=> \$DB_LOGIN,
			'where=s'			=> \$where,
			'help'				=> \$help,);

die $usage if ($help);
die "\nERROR:\n\tMissing Argument: db_login\n" .$usage
				unless ($DB_LOGIN);
die "\nERROR:\n\tMissing Argument: temp_dir\n" .$usage
				unless ($TEMP_DIR);
die "\nERROR:\n\tMissing Argument: results\n" .$usage
				unless ($results);

# remove trailing slash
$TEMP_DIR =~ s/\/$//;
				
				
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Create SQL file

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# pre-pend SQL WHERE to $where string
$where = 'WHERE '.$where unless ($where eq '');

# remove trailing slash
$TEMP_DIR =~ s/\/$//;

open (TMP_FILE, ">$TEMP_DIR/tmp.sql") or die "Couldn't open $TEMP_DIR/tmp.sql\n";
while (<DATA>)
{
	s/TEMP_DIR/$TEMP_DIR/;
	s/WHERE/$where/;
	print TMP_FILE;
}
close TMP_FILE;



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Execute SQL file and cleanup

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
my($s, $m, $h) = localtime(time);
print STDERR "\n", ("*" x 70) ,"\n";
printf STDERR "Obtain  sequences from postgreSQL.   Start  at %02d:%02d:%02d\n", $h,$m,$s;
system ("psql ".$DB_LOGIN. " -t -F \"\t\" -A -q -f $TEMP_DIR/tmp.sql");
($s, $m, $h) = localtime(time);
printf STDERR "Obtain  sequences from postgreSQL.   Finish at %02d:%02d:%02d\n", $h,$m,$s;

system ("panda_output_to_fasta ".	
		"$TEMP_DIR/panda_seq.fa.tmp ".
		"$results");

($s, $m, $h) = localtime(time);
printf STDERR "Convert sequences into FASTA format. Finish at %02d:%02d:%02d\n", $h,$m,$s;
print STDERR "\n", ("*" x 70) ,"\n";
system ("rm -f $TEMP_DIR/panda_seq.fa.tmp");
system ("rm $TEMP_DIR/tmp.sql");




#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	SQL instructions for PSQL follows

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

__DATA__
\o TEMP_DIR/panda_seq.fa.tmp
SELECT	
		pi1,
		pi2,
		sequence
			FROM sequences
			WHERE ;
\o
